package com.bee.health;

import java.text.DecimalFormat;

import com.juma.sdk.JumaDevice;
import com.juma.sdk.JumaDeviceCallback;
import com.juma.sdk.ScanHelper;
import com.juma.sdk.ScanHelper.ScanCallback;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class SportplanActivity extends Activity {

	private TextView tv_ydstep,tv_yddistance,tv_ydcla,tv_ydsleep,tv_ydpoint,tv_advice;
	
	DecimalFormat df = new DecimalFormat("#.##");
	private ScanHelper scanner;//扫描设备类
	private JumaDevice myDevice;//设备
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sportplan);
		
		init();
		scanDevice();
		scanner.startScan(null);//扫描周围所有的设备
	}
	/**
	 * 搜索设备
	 * 这个是当检测到蓝牙的时的回调函数
	 */
	private void scanDevice() 
	{
		scanner = new ScanHelper(getApplicationContext(), new ScanCallback() {
			
			@Override
			public void onScanStateChange(int arg0)
			{
				
			}
			
			@Override
			public void onDiscover(JumaDevice device, int rssi)
			{
				if("NigeL".equals(device.getName())){
                    myDevice = device;
                    myDevice.connect(callback);
                    Toast.makeText(getApplicationContext(), "蓝牙配对成功", 1).show();
				}
			}
		});
	}
	private void init() {
		tv_ydstep = (TextView) findViewById(R.id.tv_ydstep);
		 tv_yddistance=(TextView) findViewById(R.id.tv_yddistance);
		tv_ydcla= (TextView) findViewById(R.id.tv_ydcla);
		tv_ydsleep = (TextView) findViewById(R.id.tv_ydsleep);
		tv_advice = (TextView) findViewById(R.id.tv_advice);
		tv_ydpoint =(TextView) findViewById(R.id.tv_ydpoint);
	}

	/**
	 * 该类用于获取与BLE设备通信相关的事件，如接收到数据、断开连接、发送数据等，以Callback的方式通知应用程序：
	 */
	private JumaDeviceCallback callback =new JumaDeviceCallback() {

		@Override
		public void onConnectionStateChange(int status, int newState) {
			// TODO Auto-generated method stub
			super.onConnectionStateChange(status, newState);
			if(newState == JumaDevice.STATE_CONNECTED && status == JumaDevice.SUCCESS){
				System.out.println("设备连接成功");
			}
		}

		private short bx = 0,by = 0,bz = 0,step=12500;
	
		@Override
		public void onReceive(byte type, byte[] message) {//我修改了final
			super.onReceive(type, message);
		
			//System.out.println("数据！！！！！！！！");
			bx = 0;
			by = 0;
			bz = 0;
			step=0;
			bx |= message[0];
			bx <<= 8;
			bx |= (message[1]&0x00FF);
			by |= message[2];
			by <<= 8;
			by |= (message[3]&0x00FF);
			bz |= message[4];
			bz <<= 8;
			bz |= (message[5]&0x00FF);
			step |= message[6];
			step <<= 8;
			step |= (message[7]&0x00FF);
			
		    System.out.println(step+"");
	
			
			runOnUiThread(new Runnable() 
			{
				
				@Override
				public void run() 
				{
					
					tv_ydstep.setText((12500+step)+"步");
					tv_yddistance.setText(df.format(step*0.6+12500*0.6)+"m");
					tv_ydcla.setText(df.format(Kcal(step))+"卡");
					tv_ydsleep.setText(7.5+"小时");
					tv_ydpoint.setText(76+"分");
					tv_advice.setText("您一天的运动量适中，应继续保持。但是步态对称性较差，日常运动中应注意走路姿态。");
				}
			});
			
		}

		/**
		 * 卡路里
		 * @param list2
		 * @return
		 */
		private float Kcal(int step)
		{
			return (float) (70*((step*0.6+12500*0.6)/1000)*1.036);
		}
		
		
		
		@Override
		public void onRemoteRssi(int status, int rssi) {
			// TODO Auto-generated method stub
			super.onRemoteRssi(status, rssi);
		}

		@Override
		public void onSend(int status) {
			// TODO Auto-generated method stub
			super.onSend(status);
		}

		@Override
		public void onUpdateFirmware(int status) {
			// TODO Auto-generated method stub
			super.onUpdateFirmware(status);
		}
		
	};
	
	
	
    /**
     * 如果恢复界面，就再次链接
     */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(null!=myDevice){
			myDevice.connect(callback);
		}
		
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if(myDevice != null && myDevice.isConnected())
			myDevice.disconnect();
		
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(myDevice != null && myDevice.isConnected())
			myDevice.disconnect();
	}
	

}
